﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Catchbase.TestTools.UTest
{
    [TestClass]
    public class TestBaseTestBase : TestBase<TestBaseTestBase.Factory>
    {
        protected override void InitializeTest()
        {
            FuncMock<Command>();
        }

        [TestMethod]
        public void Fetch_MustExecuteCommand()
        {
            Subject.Fetch();
            Mock<Command>().Verify(c => c.Execute(), Times.Once());
        }

        [TestMethod]
        public void Fetch_MustReturnStringFromFactoryB()
        {
            Mock<FactoryB>().Setup(f => f.Create()).Returns("C");
            Subject.Fetch().MustEqual("C");
        }

        #region TestclassHelpers

        public class Command
        {
            public virtual void Execute()
            {
            }
        }

        public class Factory
        {
            private readonly Func<Command> _createCommand;
            private readonly FactoryB _factoryB;

            public Factory(FactoryB factoryB, Func<Command> createCommand)
            {
                _createCommand = createCommand;
                _factoryB = factoryB;
            }

            public String Fetch()
            {
                Command command = _createCommand();
                command.Execute();
                return _factoryB.Create();
            }
        }

        public class FactoryB
        {
            public virtual string Create()
            {
                return "B";
            }
        }

        #endregion
    }
}